package jdbcEx.examples;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import jdbcEx.examples.util.ConectionManager;

public class PrepareStatmentEx {

	public static void main(String[] args) {

		new PrepareStatmentEx().getUserDetails("two", "two");
	}

	public void getUserDetails(String userName, String password) {
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		try {

			Connection connection = ConectionManager.getInstance()
					.getConnection();

			preparedStatement = connection
					.prepareStatement("SELECT * FROM ADMIN where userName =?  and password = ?");

			preparedStatement.setString(1, userName);
			preparedStatement.setString(2, password);

			resultSet = preparedStatement.executeQuery();

			while (resultSet.next()) {

				System.out.println(" Admin Id   -  "
						+ resultSet.getString("adminId"));

			}

		} catch (Exception e) {
			System.out.println("Opps Exception occured -");
		} finally {
			try {

				if (resultSet != null) {
					resultSet.close();
				}
				if (preparedStatement != null) {
					preparedStatement.close();
				}

			} catch (Exception exception) {
				System.out
						.println("Opps Exception occured while closing resources-");
			}

		}

	}
}
